/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.manage.bioperateddata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.manage.bicouponwrite.service.BiCouponWriteService;
import vip.xiaonuo.modular.manage.bioperateddata.Listener.BiOperatedDataListener;
import vip.xiaonuo.modular.manage.bioperateddata.entity.BiOperatedData;
import vip.xiaonuo.modular.manage.bioperateddata.enums.BiOperatedDataExceptionEnum;
import vip.xiaonuo.modular.manage.bioperateddata.mapper.BiOperatedDataMapper;
import vip.xiaonuo.modular.manage.bioperateddata.param.BiOperatedDataParam;
import vip.xiaonuo.modular.manage.bioperateddata.service.BiOperatedDataService;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.service.SysOrgService;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * 门店数据service接口实现类
 *
 * @author zj
 * @date 2022-09-06 14:47:10
 */
@Service
public class BiOperatedDataServiceImpl extends ServiceImpl<BiOperatedDataMapper, BiOperatedData> implements BiOperatedDataService {

    @Resource
    private BiOperatedDataService biOperatedDataService;

    @Resource
    private SysOrgService sysOrgService;

    @Resource
    private BiCouponWriteService biCouponWriteService;

    @Override
    public PageResult<BiOperatedData> page(BiOperatedDataParam biOperatedDataParam) {
        QueryWrapper<BiOperatedData> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(biOperatedDataParam)) {

            // 根据名称 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getStoreName())) {
                queryWrapper.lambda().like(BiOperatedData::getStoreName, biOperatedDataParam.getStoreName());
            }
            // 根据详细地址 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getAddress())) {
                queryWrapper.lambda().like(BiOperatedData::getAddress, biOperatedDataParam.getAddress());
            }
            // 根据经度 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getLng())) {
                queryWrapper.lambda().eq(BiOperatedData::getLng, biOperatedDataParam.getLng());
            }
            // 根据纬度 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getLat())) {
                queryWrapper.lambda().eq(BiOperatedData::getLat, biOperatedDataParam.getLat());
            }
            // 根据电话号码 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getPhoneNumber())) {
                queryWrapper.lambda().eq(BiOperatedData::getPhoneNumber, biOperatedDataParam.getPhoneNumber());
            }
            // 根据省份 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getProvName())) {
                queryWrapper.lambda().eq(BiOperatedData::getProvName, biOperatedDataParam.getProvName());
            }
            // 根据城市 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getCityName())) {
                queryWrapper.lambda().eq(BiOperatedData::getCityName, biOperatedDataParam.getCityName());
            }
            // 根据区/县 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getCountryName())) {
                queryWrapper.lambda().eq(BiOperatedData::getCountryName, biOperatedDataParam.getCountryName());
            }
            // 根据店号 查询
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getStoreId())) {
                queryWrapper.lambda().eq(BiOperatedData::getStoreId, biOperatedDataParam.getStoreId());
            }
            // 查询类型
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getOrgCode())) {
                queryWrapper.lambda().eq(BiOperatedData::getOrgCode, biOperatedDataParam.getOrgCode());
            }
            if (ObjectUtil.isNotEmpty(biOperatedDataParam.getUserId())) {
                queryWrapper.lambda().eq(BiOperatedData::getUserId, biOperatedDataParam.getUserId());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BiOperatedData> list(BiOperatedDataParam biOperatedDataParam) {
        return this.list();
    }

    @Override
    public void add(BiOperatedDataParam biOperatedDataParam) {
        BiOperatedData biOperatedData = new BiOperatedData();
        BeanUtil.copyProperties(biOperatedDataParam, biOperatedData);
        this.save(biOperatedData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BiOperatedDataParam> biOperatedDataParamList) {
        biOperatedDataParamList.forEach(biOperatedDataParam -> {
            this.removeById(biOperatedDataParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BiOperatedDataParam biOperatedDataParam) {
        BiOperatedData biOperatedData = this.queryBiOperatedData(biOperatedDataParam);
        BeanUtil.copyProperties(biOperatedDataParam, biOperatedData);
        this.updateById(biOperatedData);
    }

    @Override
    public BiOperatedData detail(BiOperatedDataParam biOperatedDataParam) {
        return this.queryBiOperatedData(biOperatedDataParam);
    }

    /**
     * 获取门店数据
     *
     * @author zj
     * @date 2022-09-06 14:47:10
     */
    private BiOperatedData queryBiOperatedData(BiOperatedDataParam biOperatedDataParam) {
        BiOperatedData biOperatedData = this.getById(biOperatedDataParam.getId());
        if (ObjectUtil.isNull(biOperatedData)) {
            throw new ServiceException(BiOperatedDataExceptionEnum.NOT_EXIST);
        }
        return biOperatedData;
    }

    @Override
    public void export(BiOperatedDataParam biOperatedDataParam) {
        List<BiOperatedData> list = this.list(biOperatedDataParam);
        PoiUtil.exportExcelWithStream("SnowyBiOperatedData.xls", BiOperatedData.class, list);
    }


    @Override
    public void upload(BiOperatedDataParam biOperatedDataParam , BiOperatedDataService biOperatedDataService){
        SysOrg byId;
        if (!"undefined".equals(biOperatedDataParam.getOrgId())){
             byId = sysOrgService.getById(Long.valueOf(biOperatedDataParam.getOrgId()));
            if (StringUtils.isNotBlank(byId.getCode())){
                biOperatedDataParam.setOrgCode(byId.getCode());
            }
        }
        try {
            if (ObjectUtil.isNotNull(biOperatedDataParam)){
                InputStream inputStream = biOperatedDataParam.getFile().getInputStream();
                EasyExcel.read(inputStream, BiOperatedData.class ,new BiOperatedDataListener(biOperatedDataService,biOperatedDataParam.getOrgCode())).sheet().doRead();
            }
        }catch (Exception e) {
            log.error("异常信息:"+ e);
        }
    }

}
